package ifi.card.main;

import ifi.card.joueur.Player;
import ifi.card.poker.Poker;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import Definir.DefinirPokey;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class ViewGame extends SurfaceView implements SurfaceHolder.Callback,
		Runnable, OnTouchListener, DefinirPokey {
	private SurfaceHolder surfaceHolder = getHolder();
	private ScheduledExecutorService executor;
	private Player player;
	private Canvas canvas;
	static Bitmap bitmapBackGround;

	List<Poker> pokersSelected = new ArrayList<Poker>();
	DrawButton drawButton = new DrawButton();
	Bitmap bitmapMatier;
	Bitmap bitmapPoker;
	Bitmap bitmapJQK;
	Bitmap bitmapButton1;
	Bitmap bitmapMotion;
	Poker pocker;

	private int statusOfGame, statusPre, statusOfButton1;
	private boolean isMotion;

	private void loadResource(Resources res) {
		pocker = new Poker();
		bitmapBackGround = BitmapFactory.decodeResource(res,
				R.drawable.background_game);
		bitmapMatier = BitmapFactory.decodeResource(res, R.drawable.matier);
		bitmapPoker = BitmapFactory.decodeResource(res, R.drawable.poker);
		bitmapJQK = BitmapFactory.decodeResource(res, R.drawable.jqk);
		bitmapButton1 = BitmapFactory
				.decodeResource(res, R.drawable.buttongame);
		bitmapMotion = BitmapFactory.decodeResource(res, R.drawable.motion);
	}

	public void initGame() {
		player = new Player();
		player.initPlayer();
		statusPre = STATUS_EMPTY;
		statusOfGame = STATUS_EMPTY;
		statusOfButton1 = 0;
		isMotion = false;
	}

	public ViewGame(Context context) {
		super(context);
		this.getHolder().addCallback(this);
		this.setFocusable(true);
		this.requestFocus();
		this.setOnTouchListener(this);
		Resources res = getResources();
		loadResource(res);
		initGame();
	}

	@Override
	public void run() {
		try {
			canvas = surfaceHolder.lockCanvas();
			if (canvas == null)
				return;
			synchronized (surfaceHolder) {
				drawGame();
			}
		} finally {
			if (canvas != null)
				surfaceHolder.unlockCanvasAndPost(canvas);
		}
	}

	private void drawGame() {
		canvas.drawBitmap(bitmapBackGround, 0, 0, null);
		if (isMotion)
			canvas.drawBitmap(bitmapMotion, 0, 0, null);
		player.drawAllPokers(canvas, bitmapPoker, bitmapMatier, bitmapJQK);
		drawButton.drawButton(canvas, bitmapButton1, bitmapButton1,
				statusOfButton1, 1);

	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		executor = Executors.newSingleThreadScheduledExecutor();
		executor.scheduleAtFixedRate(this, TIMEDELAY, TIMEDELAY,
				TimeUnit.MILLISECONDS);
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		executor.shutdown();
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		switch (statusOfGame) {
		case STATUS_EMPTY:
			selectCard(arg0, arg1);
			selectButton(arg0, arg1);
			break;
		case STATUS_SELECT_BUTTON:
			upButton(arg0, arg1);
			break;
		case STATUS_SELECT_POKER:
			selectCard(arg0, arg1);
			selectButton(arg0, arg1);
			updateMoveCard(arg0, arg1);
			updateUpCard(arg0, arg1);
			break;

		default:
			break;
		}
		updateMoveCard(arg0, arg1);
		return true;
	}

	// == Di chuyen quan bai

	private int selectButton(View arg0, MotionEvent arg1) {
		int x, y;
		if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
			x = (int) arg1.getX();
			y = (int) arg1.getY();
			if ((y < 400) && (y > 300)) {
				if ((x < 150) && (x > 10)) {
					statusOfButton1 = 1;
					statusOfGame = STATUS_SELECT_BUTTON;
				}
				if ((x < 300) && (x > 150)) {
					statusOfButton1 = 2;
					statusOfGame = STATUS_SELECT_BUTTON;
				}
				if ((x < 650) && (x > 500)) {
					statusOfButton1 = 3;
					statusOfGame = STATUS_SELECT_BUTTON;
				}
				if ((x < 800) && (x > 650)) {
					statusOfButton1 = 4;
					statusOfGame = STATUS_SELECT_BUTTON;
				}
			}
		}
		return 0;
	}

	private void upButton(View arg0, MotionEvent arg1) {
		if (arg1.getAction() == MotionEvent.ACTION_UP) {
			if ((statusOfButton1 == 4)) {
				player.unSelectPoker(pokersSelected, true);
				statusPre = statusOfGame;
				statusOfGame = STATUS_EMPTY;
				statusOfButton1 = 0;
				statusPre = STATUS_EMPTY;
			}
			if ((statusOfButton1 == 3)) {
				player.unSelectPoker(pokersSelected, false);
				statusPre = statusOfGame;
				statusOfGame = STATUS_EMPTY;
				statusOfButton1 = 0;
				player.initPosition();
				statusPre = STATUS_EMPTY;
			}

			if ((statusOfButton1 == 1)) {
				if (statusPre != STATUS_SELECT_POKER) {
					player.sortPokers(player.pokersOfPlayer);
					statusPre = statusOfGame;
				}

				statusOfGame = STATUS_EMPTY;
				statusOfButton1 = 0;
				player.initPosition();
			}

			if ((statusOfButton1 == 2)) {
				if (statusOfGame != STATUS_SELECT_POKER) {
					statusPre = statusOfGame;
					statusOfGame = STATUS_EMPTY;
					statusOfButton1 = 0;
				}
			}
		}
	}

	private void selectCard(View arg0, MotionEvent arg1) {
		int x, y;
		if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
			x = (int) arg1.getX();
			y = (int) arg1.getY();
			if (y < 400)
				return;
			pokersSelected = player.selectPoker(x, y, pokersSelected);
			statusPre = statusOfGame;
			statusOfGame = STATUS_SELECT_POKER;
		}
	}

	private void updateMoveCard(View arg0, MotionEvent arg1) {
		int x, y;
		if (arg1.getAction() == MotionEvent.ACTION_UP) {
			x = (int) arg1.getX();
			y = (int) arg1.getY();
			if (statusPre == STATUS_SELECT_POKER)
				if (y < 100) {
					player.unSelectPoker(pokersSelected, true);
				} else if (y < 200) {
					player.unSelectPoker(pokersSelected, false);
				}
		}
		//isMotion = false;
	}

	private void updateUpCard(View arg0, MotionEvent arg1) {
		int x, y;
		if (arg1.getAction() == MotionEvent.ACTION_MOVE) {
			x = (int) arg1.getX();
			y = (int) arg1.getY();
			if (y < 100) {
				isMotion = true;
				Log.e("asdf","adsfadf");
			} else if (y < 200) {
				isMotion = false;
			}
			player.movePoker(x, y, pokersSelected);
		}

	}

	public static void deleteObject() {
		bitmapBackGround = null;
		System.gc();
	}
}
